

package pro.cherish.knowledgedemothymeleaf.core;

import cn.hutool.core.io.IoUtil;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WkHtmlToPdfUtil {
    private static final Logger log = LoggerFactory.getLogger(WkHtmlToPdfUtil.class);

    public WkHtmlToPdfUtil() {
    }

    public static void htmlToPdfFromString(WrapperConfig config, String sourceString, String targetFile) {
        try {
            Pdf pdf = new Pdf(config);
            pdf.addPageFromString(sourceString);
            pdf.saveAs(targetFile);
        } catch (InterruptedException | IOException var4) {
            log.error("wkHtmlToPdf发生异常：{}", var4);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var4.getMessage());
        }
    }

    public static void htmlToPdfFromUrl(WrapperConfig config, String soureUrl, String targetFile) {
        try {
            Pdf pdf = new Pdf(config);
            pdf.addPageFromUrl(soureUrl);
            pdf.saveAs(targetFile);
        } catch (InterruptedException | IOException var4) {
            log.error("wkHtmlToPdf发生异常：{}", var4);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var4.getMessage());
        }
    }

    public static void htmlToPdfFromFile(WrapperConfig config, String sourceFile, String targetFile) {
        try {
            Pdf pdf = new Pdf(config);
            pdf.addPageFromFile(sourceFile);
            pdf.saveAs(targetFile);
        } catch (InterruptedException | IOException var7) {
            log.error("wkHtmlToPdf发生异常：{}", var7);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var7.getMessage());
        } finally {
            ;
        }
    }

    public static byte[] htmlToPdfFromString(WrapperConfig config, String sourceString) {
        String tempPath = "";
        boolean var15 = false;

        byte[] var7;
        try {
            var15 = true;
            File temp = File.createTempFile("wkhtmltopdf" + UUID.randomUUID().toString(), ".pdf");
            Pdf pdf = new Pdf(config);
            pdf.addPageFromString(sourceString);
            pdf.saveAs(temp.getPath());
            FileInputStream inputSteam = IoUtil.toStream(temp);
            byte[] returnBytes = IoUtil.readBytes(inputSteam);
            IoUtil.close(inputSteam);
            var7 = returnBytes;
            var15 = false;
        } catch (InterruptedException | IOException var18) {
            log.error("wkHtmlToPdf发生异常：{}", var18);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var18.getMessage());
        } finally {
            if (var15) {
                try {
                    Path p = Paths.get(tempPath);
                    log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
                } catch (IOException var16) {
                    log.warn("Couldn't delete temp file " + tempPath);
                }

            }
        }

        try {
            Path p = Paths.get(tempPath);
            log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
        } catch (IOException var17) {
            log.warn("Couldn't delete temp file " + tempPath);
        }

        return var7;
    }

    public static byte[] htmlToPdfFromUrl(WrapperConfig config, String sourceUrl) {
        String tempPath = "";
        boolean var15 = false;

        byte[] var7;
        try {
            var15 = true;
            File temp = File.createTempFile("wkhtmltopdf" + UUID.randomUUID().toString(), ".pdf");
            Pdf pdf = new Pdf(config);
            pdf.addPageFromUrl(sourceUrl);
            pdf.saveAs(temp.getPath());
            tempPath = temp.getPath();
            FileInputStream inputSteam = IoUtil.toStream(temp);
            byte[] returnBytes = IoUtil.readBytes(inputSteam);
            IoUtil.close(inputSteam);
            var7 = returnBytes;
            var15 = false;
        } catch (InterruptedException | IOException var18) {
            log.error("wkHtmlToPdf发生异常：{}", var18);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var18.getMessage());
        } finally {
            if (var15) {
                try {
                    Path p = Paths.get(tempPath);
                    log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
                } catch (IOException var16) {
                    log.warn("Couldn't delete temp file " + tempPath);
                }

            }
        }

        try {
            Path p = Paths.get(tempPath);
            log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
        } catch (IOException var17) {
            log.warn("Couldn't delete temp file " + tempPath);
        }

        return var7;
    }

    public static byte[] htmlToPdfFromFile(WrapperConfig config, String sourceFile) {
        String tempPath = "";
        boolean var15 = false;

        byte[] var7;
        try {
            var15 = true;
            File temp = File.createTempFile("wkhtmltopdf" + UUID.randomUUID().toString(), ".pdf");
            Pdf pdf = new Pdf(config);
            pdf.addPageFromFile(sourceFile);
            pdf.saveAs(temp.getPath());
            tempPath = temp.getPath();
            FileInputStream inputSteam = IoUtil.toStream(temp);
            byte[] returnBytes = IoUtil.readBytes(inputSteam);
            IoUtil.close(inputSteam);
            var7 = returnBytes;
            var15 = false;
        } catch (InterruptedException | IOException var18) {
            log.error("wkHtmlToPdf发生异常：{}", var18);
            throw new RuntimeException("wkHtmlToPdf发生异常：" + var18.getMessage());
        } finally {
            if (var15) {
                try {
                    Path p = Paths.get(tempPath);
                    log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
                } catch (IOException var16) {
                    log.warn("Couldn't delete temp file " + tempPath);
                }

            }
        }

        try {
            Path p = Paths.get(tempPath);
            log.debug("Delete temp file at: " + tempPath + " " + Files.deleteIfExists(p));
        } catch (IOException var17) {
            log.warn("Couldn't delete temp file " + tempPath);
        }

        return var7;
    }
}
